// packageD/warranty/warrantyind/warrantyind.js
var app = getApp();
import util from "../../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show1: false,
    show2: false,
    datas: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initData();
  },
  goPay(e) {
    wx.navigateTo({
      url: '/packageD/warranty/policyPay/policyPay?id=' + e.currentTarget.dataset.id + "&supplier_id=" + e.currentTarget.dataset.supplier_id,
    });
  },
  edit(e) {
    wx.navigateTo({
      url: '/packageD/warranty/Edit_Balance_manage/Edit_Balance_manage?id=' + e.currentTarget.dataset.id,
    });
  },
  delete(e) {
    let that = this;

    let id = e.currentTarget.dataset.id;
    wx.showModal({
      content: '此操作将永久删除该保单, 是否继续??',
      title: '提示',
      success(res) {
        if (res.confirm) {
          let urlStr = app.getNetAddresss("plugin.supplier.frontend.insurance.insurance-del");
          urlStr += "&id=" + id;
          app._getNetWork({
            url: urlStr,
            success: function (resdata) {
              var res = resdata.data;
              console.log(res);
              if (res.result == 1) {
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 1000
                });
                that.initData();
              } else {
                wx.showToast({
                  icon: 'none',
                  title: res.msg,
                  duration: 1500
                });
              }
            },
            fail: function (res) {
              console.log(res);
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  },
  edshow(e) {
    console.log(e);
    console.log(this.data.datas);
    this.data.datas[e.currentTarget.dataset.lis].edit_active = !this.data.datas[e.currentTarget.dataset.lis].edit_active;
    this.setData({
      datas: this.data.datas
    });
  },
  tapAdd() {
    wx.navigateTo({
      url: '/packageD/warranty/Add_Balance_manage/Add_Balance_manage',
    });
  },
  gojilu(e) {
    console.log(e.currentTarget.dataset.id);
    wx.navigateTo({
      url: '/packageD/warranty/warrantyAdd/warrantyAdd?id=' + e.currentTarget.dataset.id,
    });
  },
  TimeFilter: function(value) {
    if(!value){
      return null;
    }
    // return new Date(parseInt(value) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
    let date = new Date(value * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    let D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' ';
    let h = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours()) + ':';
    let m = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes()) + ':';
    let s = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());
    return Y+M+D+h+m+s;
  },
  lastTime: function(lastT) {
    if(!lastT){
      return null;
    }
    let timestamp = Date.parse(new Date()) / 1000;
    let total = (lastT - timestamp);
    let dataTotal = parseInt(total / (24*60*60));
    if(dataTotal){
      return dataTotal + '天后到期';//计算整数天数
    }else{
      return "已到期";
    }
  },
  initData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.supplier.frontend.insurance.index");
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          for (let i = 0; i < res.data.list.length; i++) {
            res.data.list[i].edit_active = false;
            res.data.list[i].is_time = util.formatTimeTwo(res.data.list[i].pay_time,'Y/M/D h:m:s');
            res.data.list[i].lo_time = that.lastTime(res.data.list[i].lost_time);
          }
          console.log(res.data.list);
          that.setData({
            datas: res.data.list,
            goods_length: res.data.list.length,
            pay_status: res.data.set.ins_pay_status,
            renew_status: res.data.set.ins_renew_status,
            phone1: res.data.set.ins_phone_1,
            phone_2: res.data.set.ins_phone_2
          });
          console.log(that.data.datas);
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  showpop(e) {
    // this.setData({
    //   show1: true
    // })
    let that = this;

    let id = e.currentTarget.dataset.id;
    wx.showModal({
      content: '确认续保吗?',
      title: '提示',
      success(res) {
        if (res.confirm) {
          let urlStr = app.getNetAddresss("plugin.supplier.frontend.insurance.contInsurance");
          urlStr += "&ins_id=" + id;
          app._getNetWork({
            url: urlStr,
            success: function (resdata) {
              var res = resdata.data;
              console.log(res);
              if (res.result == 1) {
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 1000
                });
                that.initData();
              } else {
                wx.showToast({
                  icon: 'none',
                  title: res.msg,
                  duration: 1500
                });
              }
            },
            fail: function (res) {
              console.log(res);
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  },
  isCoverLayer() {
    this.setData({
      show1: false,
      show2: false
    });
  },
  tapdel() {
    this.setData({
      show2: false
    });
  },
  callphoto() {
    // wx.makePhoneCall({
    //   phoneNumber: '13437845476',
    // })
    this.setData({
      show2: true,
      show1: false
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});